package com.canbot.u05.activity.reception;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.adapter.UsherModeChooseAudioListViewAdapter;
import com.canbot.u05.bean.Audio;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * 迎宾模式选择音频界面
 */
public class ReceptionModeChooseAudioActivity extends ReceptionBaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

        private ImageView ivBack;

        private ImageView btnSure;

        private ImageView btnCancle;

        private ListView listView;

        private UsherModeChooseAudioListViewAdapter adapter;

        private List<Audio> data = new ArrayList<>();

        private int lastPosition = -1;

        private Audio selectAudio;  //选择的音频

        private int type;

        private String voiceContent;

        private ToastTools toastTools;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_reception_mode_choose_audio);
                type = getIntent().getIntExtra("type", 0);
                voiceContent = getIntent().getStringExtra("voiceContent");
                toastTools = ToastTools.newInstance(this);
                setViews();
                getData(type);
        }

        private void setViews() {
                listView = (ListView) findViewById(R.id.usher_mode_choose_audio_listview);
                ivBack = (ImageView) findViewById(R.id.imageView_back);
                btnSure = (ImageView) findViewById(R.id.usher_mode_choose_audio_imageView_sure);
                btnCancle = (ImageView) findViewById(R.id.usher_mode_choose_audio_imageView_cancle);
                ivBack.setOnClickListener(this);
                btnSure.setOnClickListener(this);
                btnCancle.setOnClickListener(this);
                listView.setOnItemClickListener(this);
        }

        private void setAdapter(List<Audio> a) {
                adapter = new UsherModeChooseAudioListViewAdapter(this, a);
                listView.setAdapter(adapter);
        }


        private void getData(int type) {
                String url = "http://" + IpMsgConst.SOCKET_HOST + ":7755/getAudioList";
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("path", type == 0 ? PathConst.USHER_ZHY_DIR : "/uusound");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("t", result);
                                JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                final List<Audio> a = JSON.parseArray(strMsgBean.getResult().toString(), Audio.class);
                                Collections.sort(a, new Comparator<Audio>() {
                                        @Override
                                        public int compare(Audio audio, Audio t1) {
                                                if (audio.getName().equals(voiceContent)) {
                                                        audio.setChecked(true);
                                                }
                                                return audio.getName().compareTo(t1.getName());
                                        }
                                });
                                data.addAll(a);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                setAdapter(data);

                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                toastTools.showText(getString(R.string.network_error));
                        }
                });
        }


        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.usher_mode_choose_audio_imageView_sure:
                                //确定
                                if (selectAudio == null) {
                                        toastTools.showText(getString(R.string.please_select_audio));
                                        return;
                                }
                                String audioPath = selectAudio.getPath();
                                Intent intent = new Intent();
                                intent.putExtra("voiceType", type);
                                intent.putExtra("voiceContent", audioPath);
                                setResult(RESULT_OK, intent);
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.usher_mode_choose_audio_imageView_cancle:
                                //取消
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;

                }
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (data.get(position).isChecked()) {
                        data.get(position).setChecked(false);
                        adapter.updataView(position, listView);
                        selectAudio = null;
                        MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop");
                }
                else {
                        data.get(position).setChecked(true);
                        adapter.updataView(position, listView);
                        selectAudio = data.get(position);
                        String audioPath = selectAudio.getPath();
                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, audioPath);
                }
                if (lastPosition != -1
                        && data.get(lastPosition).isChecked()
                        && lastPosition != position) {
                        data.get(lastPosition).setChecked(false);
                        adapter.updataView(lastPosition, listView);
                }
                lastPosition = position;
        }
}
